package com.lhn.controller;

import com.lhn.entity.ResponseMsg;
import com.lhn.entity.User;
import com.lhn.feign.UserFeign;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lhn
 * @date 2024-10-12 10:12
 */

@Controller
@RequestMapping("consumer/user/")
public class UserController {

    @Autowired
    private UserFeign userFeign;

    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location) {
        return location;
    }

    @GetMapping("findAll")
    @ResponseBody
    public ResponseMsg<List<User>> findAll(@Param("page") int page, @Param("limit") int limit) {
        int index = (page - 1) * limit;
        return new ResponseMsg<>("0", "", userFeign.count(), userFeign.findAll(index, limit));
    }

    @GetMapping("findById/{id}")
    public User findById(@PathVariable("id") long id) {
        return userFeign.findById(id);
    }

    @GetMapping("count")
    public int count() {
        return userFeign.count();
    }

    @GetMapping("deleteById/{id}")
    public String deleteById(@PathVariable("id") long id) {
        System.out.println("deleteById is " + id);
        userFeign.deleteById(id);
        return "redirect:/consumer/user/redirect/user_manage";
    }

    @PostMapping("save")
    public String save(User user) {
        userFeign.save(user);
        return "redirect:/consumer/user/redirect/user_manage";
    }

    @PutMapping("update")
    public void update(User user) {
        userFeign.update(user);
    }
}
